export const useRuleUtils = () => {
  const errorNecessaryMsg = '这是必填项';
  return {
    stuId: (rule: any, value: string, callback: any) => {
      if (!value || value === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (value.substring(0, 3) !== 'STU') {
        callback(new Error('必须以STU开头'));
      } else {
        callback();
      }
    },
    workId: (rule: any, value: string, callback: any) => {
      if (!value || value === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (value.substring(0, 3) !== 'TCH') {
        callback(new Error('必须以TCH开头'));
      } else {
        callback();
      }
    },
    identityId: (rule: any, value: string, callback: any) => {
      if (!value || value === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (!/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(value)) {
        callback(new Error('身份证不合法'));
      } else {
        callback();
      }
    },
    integer: (rule: any, value: string, callback: any) => {
      if (!value || String(value) === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (!/^\d+$/.test(String(value))) {
        callback(new Error('只能是正整数'));
      } else {
        callback();
      }
    },
    qqNum: (rule: any, value: string, callback: any) => {
      if (!value || value === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (!/^[1-9][0-9]{4,12}$/.test(value)) {
        callback(new Error('QQ号不合法'));
      } else {
        callback();
      }
    },
    phone: (rule: any, value: string, callback: any) => {
      if (!value || value === '') {
        callback(new Error(errorNecessaryMsg));
      } else if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value)) {
        callback(new Error('手机号不合法'));
      } else {
        callback();
      }
    },
    password: (rule: any, value: any, callback: any) => {
      if (!value || value === '') {
        callback(new Error('请输入密码'));
      } else if (!/^\S*(?=\S{6,})\S*$/.test(value)) {
        callback(new Error('密码长度不小于6位'));
      } else {
        callback();
      }
    },
  };
};
